﻿using System;
using System.Collections.Generic;
using CSDemo.Common;
using Hik.Communication.ScsServices.Service;

namespace CSDemo.SCS.Server
{
    public class ScsProductService : ScsService, IProductService
    { 
        private readonly SortedList<string, Product> _records;
         
        public ScsProductService()
        {
            _records = new SortedList<string, Product>();
        }
        
        public bool AddProduct(Product product)
        {
            if (product == null)
            {
                throw new ArgumentNullException("product");
            }
            _records[product.Name] = product;
            return true;
        }

        public bool DeleteProduct(string name)
        {
            if (!_records.ContainsKey(name))
            {
                return false;
            }

            _records.Remove(name);
            return true;
        }

        public Product GetProduct(string name)
        {
            if (_records.ContainsKey(name))
            {
                return _records[name];
            }
             
            foreach (var record in _records)
            {
                if (record.Key.ToLower().Contains(name.ToLower()))
                {
                    return record.Value;
                }
            }
            return null;
        }
    }
}